package com.bjp.seekersprofile.action;

import java.util.Calendar;
import java.util.Date;
import java.util.List;

import javax.portlet.ActionRequest;
import javax.portlet.ActionResponse;
import javax.portlet.PortletConfig;
import javax.portlet.PortletSession;
import javax.portlet.PortletURL;
import javax.portlet.RenderRequest;
import javax.portlet.RenderResponse;
import javax.servlet.http.HttpServletRequest;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.bjp.seekersprofile.model.Profile;
import com.bjp.seekersprofile.model.Qualification;
import com.bjp.seekersprofile.model.impl.ProfileImpl;
import com.bjp.seekersprofile.model.impl.QualificationImpl;
import com.bjp.seekersprofile.service.ProfileLocalServiceUtil;
import com.bjp.seekersprofile.service.ProfileServiceUtil;
import com.liferay.portal.ContactBirthdayException;
import com.liferay.portal.kernel.util.Constants;
import com.liferay.portal.kernel.util.ParamUtil;
import com.liferay.portal.kernel.util.Validator;
import com.liferay.portal.model.User;
import com.liferay.portal.struts.PortletAction;
import com.liferay.portal.theme.ThemeDisplay;
import com.liferay.portal.util.PortalUtil;
import com.liferay.portlet.ActionResponseImpl;
import com.bjp.seekersprofile.service.QualificationLocalServiceUtil;
import com.bjp.image.process.AddImageProcess;


public class SeekersProfileAction extends PortletAction {
	private static final Log log = LogFactory.getLog(SeekersProfileAction.class);

	public void processAction(ActionMapping mapping, ActionForm form,
			PortletConfig config, ActionRequest req, ActionResponse res)
			throws Exception {

		String cmd = ParamUtil.getString(req, "cmd");
		if (cmd.equals("ADD")) {
			add(mapping, form, config, req, res);
		}
		else if (cmd.equals("UPDATE")) {
			update(mapping, form, config, req, res);
			//setForward(req, "bjp.seekersprofile.add");
		} else if (cmd.equals("ADD_EXTRA")) {
			addExtra(mapping, form, config, req, res);
		}
	}

	private void add(ActionMapping mapping, ActionForm form,
			PortletConfig config, ActionRequest req, ActionResponse res)
			throws Exception {

		log.info("Inside SeekersProfileAction file - Method: loadpage");

		HttpServletRequest httpReq = PortalUtil.getHttpServletRequest(req);
		
		long profileId = ParamUtil.getLong(req, "profileId");
		Profile profile = null;
		
		Long userId = PortalUtil.getUserId(req);
		PortletSession ps = req.getPortletSession();
		String tabValue = "extra";
		String tabName = "Extra";

		String firstName = ParamUtil.getString(req, "firstName");
		String lastName = ParamUtil.getString(req, "lastName");
		boolean gender = ParamUtil.getBoolean(req, "gender");
		String mobile = ParamUtil.getString(req, "mobile");
		String homePhone = ParamUtil.getString(req, "homePhone");
		//int birthDate = ParamUtil.getInteger(req, "birthDate");
		//int birthMonth = ParamUtil.getInteger(req, "birthMonth");
		//int birthYear = ParamUtil.getInteger(req, "birthYear");
		String address = ParamUtil.getString(req, "address");
		String emailAddress = ParamUtil.getString(req, "emailAddress");
		String currentLocation = ParamUtil.getString(req, "currentLocation");
		String jobLocation = ParamUtil.getString(req, "jobLocation");
		String totalExperience = ParamUtil.getString(req, "totalExperience");
		String jobCategory = ParamUtil.getString(req, "jobCategory");
		String keySkills = ParamUtil.getString(req, "keySkills");
		String highestQualification = ParamUtil.getString(req, "highestQualification");
		String specialization = ParamUtil.getString(req, "specialization");
		String institute = ParamUtil.getString(req, "institute");
		String resumeTitle = ParamUtil.getString(req, "resumeTitle");
		String up_resume = ParamUtil.getString(req, "up_resume");
		
		profile = new ProfileImpl();
		
		AddImageProcess.addImage(req, profile, "photo");
		
		profile.setFirstName(firstName);
		profile.setLastName(lastName);
		profile.setGender(gender);
		profile.setDateAdded(new Date());
		profile.setMobile(mobile);
		profile.setHomePhone(homePhone);
		profile.setAddress(address);
		profile.setEmailAddress(emailAddress);
		profile.setCurrentLocation(currentLocation);
		profile.setJobLocation(jobLocation);
		profile.setTotalExperience(totalExperience);
		profile.setJobCategory(jobCategory);
		profile.setKeySkills(keySkills);
		profile.setHighestQualification(highestQualification);
		profile.setSpecialization(specialization);
		profile.setInstitute(institute);
		profile.setResumeTitle(resumeTitle);
		profile.setUp_resume(up_resume);
		int birthdayMonth = ParamUtil.getInteger(req, "dateOfBirthMonth");
        int birthdayDay = ParamUtil.getInteger(req, "dateOfBirthDay");
        int birthdayYear = ParamUtil.getInteger(req, "dateOfBirthYear");
        Date birthDay = PortalUtil.getDate(
                        birthdayMonth, birthdayDay, birthdayYear,
                        new ContactBirthdayException());
        /*
		if (birthDate >= 0 && birthMonth >= 0 && birthYear >= 0) {
			Calendar now = Calendar.getInstance();
			now.set(Calendar.YEAR, birthYear);
			now.set(Calendar.MONTH, birthMonth);
			now.set(Calendar.DATE, birthDate);
			
			profile.setDateOfBirth(now.getTime());
		}
		*/
		profile.setDateAdded(new Date());
		profile.setAddedBy(userId);
		profile.setDateOfBirth(birthDay);
		
			ProfileLocalServiceUtil.getService().addProfile(profile);
		
		profile = (Profile) ProfileLocalServiceUtil.getProfile(profile.getProfileId());
		req.setAttribute("PROFILE", profile);
		req.setAttribute("TAB_VALUE", tabValue);
		req.setAttribute("TAB_NAME", tabName);
		
		setForward(req, "bjp.seekersprofile.add_qualification");
	}

	private void update(ActionMapping mapping, ActionForm form,
			PortletConfig config, ActionRequest req, ActionResponse res)
			throws Exception {

		log.info("Inside SeekersProfileAction file - Method: Update");

		HttpServletRequest httpReq = PortalUtil.getHttpServletRequest(req);
		
		long profileId = ParamUtil.getLong(req, "profileId");
		if(profileId == 0l)
			profileId = Long.parseLong(ParamUtil.getString(req, "profileId"));

		Profile profile = null;
		
		Long userId = PortalUtil.getUserId(req);
		PortletSession ps = req.getPortletSession();

		String firstName = ParamUtil.getString(req, "firstName");
		String lastName = ParamUtil.getString(req, "lastName");
		boolean gender = ParamUtil.getBoolean(req, "gender");
		String mobile = ParamUtil.getString(req, "mobile");
		String homePhone = ParamUtil.getString(req, "homePhone");
		//int birthDate = ParamUtil.getInteger(req, "birthDate", -1);
		//int birthMonth = ParamUtil.getInteger(req, "birthMonth", -1);
		//int birthYear = ParamUtil.getInteger(req, "birthYear", -1);
		String address = ParamUtil.getString(req, "address");
		String emailAddress = ParamUtil.getString(req, "emailAddress");
		String currentLocation = ParamUtil.getString(req, "currentLocation");
		String jobLocation = ParamUtil.getString(req, "jobLocation");
		String totalExperience = ParamUtil.getString(req, "totalExperience");
		String jobCategory = ParamUtil.getString(req, "jobCategory");
		String keySkills = ParamUtil.getString(req, "keySkills");
		String highestQualification = ParamUtil.getString(req, "highestQualification");
		String specialization = ParamUtil.getString(req, "specialization");
		String institute = ParamUtil.getString(req, "institute");
		String resumeTitle = ParamUtil.getString(req, "resumeTitle");
		String up_resume = ParamUtil.getString(req, "up_resume");
		
		int birthdayMonth = ParamUtil.getInteger(req, "dateOfBirthMonth");
        int birthdayDay = ParamUtil.getInteger(req, "dateOfBirthDay");
        int birthdayYear = ParamUtil.getInteger(req, "dateOfBirthYear");
        Date birthDay = PortalUtil.getDate(
                        birthdayMonth, birthdayDay, birthdayYear,
                        new ContactBirthdayException());
        
		if (profileId > 0l) {
			profile = ProfileLocalServiceUtil.getProfile(profileId);
		} else {
			profile = new ProfileImpl();
		}
		profile.setFirstName(firstName);
		profile.setLastName(lastName);
		profile.setGender(gender);
		profile.setDateAdded(new Date());
		profile.setMobile(mobile);
		profile.setHomePhone(homePhone);
		profile.setAddress(address);
		profile.setEmailAddress(emailAddress);
		profile.setCurrentLocation(currentLocation);
		profile.setJobLocation(jobLocation);
		profile.setTotalExperience(totalExperience);
		profile.setJobCategory(jobCategory);
		profile.setKeySkills(keySkills);
		profile.setHighestQualification(highestQualification);
		profile.setSpecialization(specialization);
		profile.setInstitute(institute);
		profile.setResumeTitle(resumeTitle);
		profile.setUp_resume(up_resume);		
		profile.setDateOfBirth(birthDay);
		/*
		if (birthDate >= 0 && birthMonth >= 0 && birthYear >= 0) {
			Calendar now = Calendar.getInstance();
			now.set(Calendar.YEAR, birthYear);
			now.set(Calendar.MONTH, birthMonth);
			now.set(Calendar.DATE, birthDate);
			
			profile.setDateOfBirth(now.getTime());
		}
		*/
		profile.setDateAdded(new Date());
		profile.setAddedBy(userId);
		try {
			ProfileLocalServiceUtil.getService().updateProfile(profile);
		} catch(Exception e) {
			setForward(req, "bjp.seekersprofile.add");
		}
		profile = (Profile) ProfileLocalServiceUtil.getProfile(profile.getProfileId());
		req.setAttribute("PROFILE", profile);
		
		setForward(req, "bjp.seekersprofile.view");
	}
	
	/*
	 * 
	 */
	private void addExtra(ActionMapping mapping, ActionForm form,
			PortletConfig config, ActionRequest req, ActionResponse res)
			throws Exception {

		log.info("Inside SeekersProfileAction file - Method: addExtra");

		HttpServletRequest httpReq = PortalUtil.getHttpServletRequest(req);
		PortletURL url = ((ActionResponseImpl) res).createRenderURL();

		long profileId = ParamUtil.getLong(req, "profileId");
		if(profileId == 0l)
			profileId = Long.parseLong(ParamUtil.getString(req, "profileId"));
		
		PortletSession ps = req.getPortletSession();

		String redirectURL = ParamUtil.getString(req, "redirectURL");
		
		String courseName = ParamUtil.getString(req, "courseName");
		String university = ParamUtil.getString(req, "university");
		String courseYear = ParamUtil.getString(req, "courseYear");
		boolean regular = ParamUtil.getBoolean(req, "regular");

		Qualification qualification = new QualificationImpl();

		qualification.setProfileId(profileId);
		qualification.setCourseName(courseName);
		qualification.setUniversity(university);
		qualification.setCourseYear(courseYear);
		qualification.setRegular(regular);
		
		qualification.setDateAdded(new Date());
		
		try {
			QualificationLocalServiceUtil.getService().addQualification(qualification);
		} catch(Exception e) {
			setForward(req, "bjp.seekersprofile.add_qualification");
		}
		qualification = QualificationLocalServiceUtil.getQualification(qualification.getQualificationId());
		req.setAttribute("QUALIFICATION", qualification);
		
		Profile profile = (Profile) ProfileLocalServiceUtil.getProfile(profileId);
		req.setAttribute("PROFILE", profile);
		
		//res.sendRedirect(redirectURL + "&QUALIFICATION=" + qualification + "&PROFILE=" + profile);
		
		//setForward(req, "bjp.seekersprofile.view");
		setForward(req, "bjp.seekersprofile.interaction");
	}
}